#ifndef CLIENTTHREAD_H
#define CLIENTTHREAD_H

#include <QThread>
#include <QTcpSocket>

#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtXml/QDomNode>
#include <QtXml/QDomNodeList>

#include "global.h"
#include "msgheader.h"

class ClientThread : public QThread
{
    Q_OBJECT
public:
    explicit ClientThread(QObject *parent = 0);
    ClientThread(int fd,QDomDocument userList);

signals:
    void requestSendPackage(QByteArray package);
    void NewConnection(QString uid,QString nickname,QString ip,int port);
    void DisConnection(QString uid,QString nickname,QString ip,int port);

private slots:
    ///process the client's request
    void processRegister(const MSGHeader &header,const QByteArray &data);
    void processLogin(const MSGHeader &header,const QByteArray &data);
    void processAdd(const MSGHeader &header,const QByteArray &data);
    void processDelete(const MSGHeader &header,const QByteArray &data);
    void processUpdate(const MSGHeader &header,const QByteArray &data);
    void processBrowse(const MSGHeader &header,const QByteArray &data);

    ///process the socket event
    void processConnected();
    void processDisconnected();
    void processReadyRead();

    ///interpret the package receive from client,and do something
    void interpretOperation(MSGHeader header,QByteArray data);
    void sendPackage(QByteArray package);

    void processServerClose();

public:
    void run();
private:
    QTcpSocket m_ClientConnection;

    QString m_UID;
    QString m_NickName;
    QString m_IP;
    int     m_Port;

    QDomDocument m_userList;  ///is a DOM,to hold the client list
    QDomElement  m_root;      ///DOM's root

};

#endif // CLIENTTHREAD_H
